import React, {
	PureComponent
} from 'react'

import {
	Form,
	Input
} from 'antd'
import LoginContext from './loginContext'
import ItemMap from './map'

const FormItem = Form.Item

class WrapFormItem extends PureComponent {
	getFormItemOptions = ({
		onChange,
		defaultValue,
		customprops,
		rules
	}) => {
		const options = {
			rules: rules || customprops.rules,
		};
		if (onChange) {
			options.onChange = onChange;
		}
		if (defaultValue) {
			options.initialValue = defaultValue;
		}
		return options;
	}
	render() {
		const {
			form: {
				getFieldDecorator
			}
		} = this.props
		const {
			onChange,
			customprops,
			defaultValue,
			rules,
			name,
			...restProps
		} = this.props

		const options = this.getFormItemOptions(this.props)
		const otherProps = restProps || {}
		return (
			<FormItem>{getFieldDecorator(name, options)(<Input {...customprops} {...otherProps} />)}</FormItem>
		)
	}
}

const LoginItem = {}

Object.keys(ItemMap).forEach(key => {
	const item = ItemMap[key];
	LoginItem[key] = props => (
		<LoginContext.Consumer>
			{context => (
				<WrapFormItem 
					customprops={item.props} 
					rules={item.rules}
					{...props} 
					type={key}
					form={context.form} />
			)}
		</LoginContext.Consumer>
	)
})

export default LoginItem